package com.tianluo.tianluobookstore.controller.admin;

import com.tianluo.tianluobookstore.domain.view.BookPageVO;
import com.tianluo.tianluobookstore.domain.view.BookVO;
import com.tianluo.tianluobookstore.enums.BookCodeEnum;
import com.tianluo.tianluobookstore.request.AddBookRequest;
import com.tianluo.tianluobookstore.request.QueryBookRequest;
import com.tianluo.tianluobookstore.response.BaseResponse;
import com.tianluo.tianluobookstore.service.BookService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
public class BookAdminController {

    @Autowired
    private BookService bookService;

    @PostMapping("/addBook")
    public BaseResponse<Void> addBook(@Valid @ModelAttribute AddBookRequest request) {
        int addBookResult = bookService.addBook(request);
        if (addBookResult > 0) {
            return BaseResponse.success(null);
        }
        return BaseResponse.fail(BookCodeEnum.BOOK_ADD_FAIL.getCode(), BookCodeEnum.BOOK_ADD_FAIL.getMessage());
    }

    @GetMapping("/queryBookList")
    public BaseResponse<BookPageVO> queryBookList(QueryBookRequest request) {
        return BaseResponse.success(bookService.pageQueryBookList(request));
    }

    @GetMapping("/queryBookById")
    public BaseResponse<BookVO> queryBookById(Integer bookId) {
        return BaseResponse.success(bookService.queryBookById(bookId));
    }


}
